package com.youlai.system.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.constant.SystemConstants;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.FrequencyForm;
import com.youlai.system.model.query.FrequencyPageQuery;
import com.youlai.system.model.vo.FrequencyPageVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SysDictService;
import com.youlai.system.service.SysFrequencyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评估频次控制器
 */
@Tag(name = "22.评估频次接口")
@RestController
@RequestMapping("/api/v1/frequencies")
@RequiredArgsConstructor
public class SysFrequencyController {

    private final SysFrequencyService frequencyService;

    private final SysDictService dictService;

    @Operation(summary = "评估频次分页列表")
    @GetMapping("/page")
    public PageResult<FrequencyPageVO> getFrequencyPage(
            @ParameterObject FrequencyPageQuery queryParams
    ) {
        IPage<FrequencyPageVO> result = frequencyService.getFrequencyPage(queryParams);
        result.getRecords().forEach(frequencyPageVO -> {
            frequencyPageVO.setFrequencyLabel(dictService.getLabel(SystemConstants.FREQUENCY, frequencyPageVO.getFrequency()));
        });
        return PageResult.success(result);
    }

    @Operation(summary = "新增评估频次")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:frequency:add')")
    @PreventDuplicateSubmit
    public Result saveFrequency(
            @RequestBody @Valid FrequencyForm frequencyForm
    ) {
        boolean result = frequencyService.saveFrequency(frequencyForm);
        return Result.judge(result);
    }

    @Operation(summary = "评估频次表单数据")
    @GetMapping("/{frequencyId}/form")
    public Result<FrequencyForm> getFrequencyForm(
            @Parameter(description = "评估频次ID") @PathVariable Long frequencyId
    ) {
        FrequencyForm formData = frequencyService.getFrequencyForm(frequencyId);
        return Result.success(formData);
    }


    @Operation(summary = "修改评估频次")
    @PutMapping(value = "/{frequencyId}")
    @PreAuthorize("@ss.hasPerm('sys:frequency:edit')")
    public Result updateFrequency(
            @Parameter(description = "评估频次ID") @PathVariable Long frequencyId,
            @RequestBody @Validated FrequencyForm frequencyForm) {
        boolean result = frequencyService.updateFrequency(frequencyId, frequencyForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除评估频次")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:frequency:delete')")
    public Result deleteFrequencies(
            @Parameter(description = "评估频次ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "评估频次删除数据为空");
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean result = frequencyService.deleteFrequencies(idList);
        return Result.judge(result);
    }

}
